﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using 纸片人.CalculateControls;
using 纸片人.win2dEffectControl;


namespace 纸片人.UtilFolter
{
    public static class TemplateControlUtil
    {
        #region 设置指定控件和数据的绑定

        /// <summary>
        /// 设置slider 和指定的属性进行绑定
        /// </summary>
        /// <param name="slider"></param>
        /// <param name="source"></param>
        /// <param name="propertyPath"></param>
        public static void SetTextBoxSlider(TextBoxSlider slider, object source, string propertyPath)
        {
            CreateBinding(source, "", new PropertyPath(propertyPath), BindingMode.TwoWay, slider, TextBoxSlider.ValueProperty);
        }

        public static void SetComboBox(ComboBox comboBox, object itemsSource, object source, string propertyPath)
        {

            comboBox.ItemsSource = itemsSource;
            CreateBinding(source, "", new PropertyPath(propertyPath), BindingMode.TwoWay, comboBox, ComboBox.SelectedItemProperty);
        }

        public static void SetCheckBox(CheckBox checkBox, object source, string propertyPath)
        {
            CreateBinding(source, "", new PropertyPath(propertyPath), BindingMode.TwoWay, checkBox, CheckBox.IsCheckedProperty);
        }

        public static void SetVector2Control(Vector2Control vector2Control, object source, string propertyPath)
        {
            CreateBinding(source, "", new PropertyPath(propertyPath), BindingMode.TwoWay, vector2Control, Vector2Control.Vector2Property);
        }

        public static void SetVector4Control(Vector4Control vector4Control, object source, string propertyPath)
        {
            CreateBinding(source, "", new PropertyPath(propertyPath), BindingMode.TwoWay, vector4Control, Vector4Control.Vector4Property);
        }

        public static void SetMatrix3x2Control(Matrix3x2Control matrix3x2Control, object source, string propertyPath)
        {
            CreateBinding(source, "", new PropertyPath(propertyPath), BindingMode.TwoWay, matrix3x2Control, Matrix3x2Control.Matrix3x2Property);
        }

        public static void SetRectSlider(RectSlider rectSlider, object source, string propertyPath)
        {
            CreateBinding(source, "", new PropertyPath(propertyPath), BindingMode.TwoWay, rectSlider, RectSlider.RectProperty);

        }

        public static void SetColorPicker(ColorPicker colorPicker, object source, string propertyPath)
        {
            CreateBinding(source, "", new PropertyPath(propertyPath), BindingMode.TwoWay, colorPicker, ColorPicker.ColorProperty);

        }

        /// <summary>
        /// 创建绑定
        /// </summary>
        /// <param name="source">绑定的属性</param>
        /// <param name="elementName">元素名</param>
        /// <param name="path">属性路径</param>
        /// <param name="mode">绑定模式</param>
        /// <param name="target">绑定目标</param>
        /// <param name="dp">绑定的属性类型</param>
        public static void CreateBinding(Object source, string elementName, PropertyPath path, BindingMode mode, DependencyObject target, DependencyProperty dp, IValueConverter convert = null)
        {

            // 实例化 Binding 对象
            Binding binding = new Binding()
            {
                Source = source,
                ElementName = elementName, // textBox2 是延迟加载元素，将其与 textBox1 绑定后，textBox2 就会被加载
                Path = path,
                Mode = mode,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                Converter = convert
            };

            // 将目标对象的目标属性与指定的 Binding 对象关联
            BindingOperations.SetBinding(target, dp, binding);
        }


        /// <summary>
        /// 创建绑定
        /// </summary>
        /// <param name="source">绑定的属性</param>
        /// <param name="elementName">元素名</param>
        /// <param name="path">属性路径</param>
        /// <param name="target">绑定目标</param>
        /// <param name="dp">绑定的属性类型</param>
        public static void CreateBinding(Object source, string elementName, string path, DependencyObject target, DependencyProperty dp, IValueConverter convert = null)
        {
            CreateBinding(source, elementName, new PropertyPath(path), BindingMode.TwoWay, target, dp, convert);
        }


        #endregion

     
    }
}
